import { Component, Input, OnInit, ViewChild } from "@angular/core";
import {
  FormLayout,
  DValidateRules,
  DFormControlRuleDirective,
} from "ng-devui/form";
import { ManageService } from "src/app/@core/manage.service";

@Component({
  selector: "app-collection-dialog",
  templateUrl: "./collection-dialog.component.html",
  styleUrls: ["./collection-dialog.component.less"],
})
export class CollectionDialogComponent implements OnInit {
  @Input() data: any;
  branch = "";
  collectionSymbol = "";
  collectionName = "";

  options2 = [
    {
      name: "是",
      value: 1,
    },
    {
      name: "否",
      value: 0,
    },
  ];

  currentOption4 = {};
  layoutDirection: FormLayout = FormLayout.Horizontal;
  @ViewChild("newCollectionForm")
  newCollectionFormDir: DFormControlRuleDirective;
  @Input() collectionID: string;

  constructor(private manageService: ManageService) { }

  ngOnInit(): void {
    if (this.data.collectionID) {
      this.manageService
        .collectionQuery({
          page: 1,
          pageSize: 10,
          collectionIndex: this.data.collectionID,
          collectionStatus: -1,
        })
        .subscribe((res) => {
          if (res.data.collections) {
            let obj = res.data.collections[0];
            this.branch = obj.collectionIndex;
            this.collectionSymbol = obj.collectionSymbol;
            this.collectionName = obj.collectionName;
            this.currentOption4 = this.options2.filter(
              (item) => item.value === obj.collectionStatus
            )[0];
          }
        });
    }
  }

  selectChange(item) {
    if (this.newCollectionFormDir.isReady) {
      this.data.canConfirm(true);
    }
  }
  
  isInputDisable(): boolean {
    return this.data.collectionID ? true : false;
  }

  formChange() {
    if (this.newCollectionFormDir.isReady) {
      this.data.canConfirm(true);
    } else {
      this.data.canConfirm(false);
    }
  }

  formRules: { [key: string]: DValidateRules } = {
    rule: {
      message: "请检查表单！",
      messageShowType: "text",
    },
    branch: {
      validators: [
        { required: true, message: "集合ID不能为空" },
        { minlength: 3 },
        { maxlength: 32 },
        { pattern: /^[a-zA-Z]([a-zA-Z0-9])+$/ },
      ],
      message: "请输入3-32位的数字字母组合",
    },
    collectionName: {
      validators: [
        { required: true, message: "集合名不能为空" },
        { minlength: 3 },
        { maxlength: 10 },
      ],
      message: "长度为3-10位",
    },
    collectionSymbol: {
      validators: [
        { required: true, message: "集合名不能为空" },
        { minlength: 3 },
        { maxlength: 20 },
      ],
      message: "长度为3-20位",
    },
  };
}
